package com.group.controller;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.view.BaseResult;
import com.base.view.GeneralResult;
import com.base.view.ListResult;
import com.group.model.Message;
import com.group.model.Topic;
import com.group.view.MessageView;

/**
 * 消息相关接口
 */
@Controller
public class MessageController extends BaseController {

	/**
	 * 查询是否有消息
	 * 
	 * @param userId
	 * @return
	 */
	@GetMapping("/message/count")
	@ResponseBody
	public GeneralResult<MessageView> isMessage(Long userId) {
		MessageView view = new MessageView();
		view.setFollowCount(messageDao.queryNewMessageCount(userId, 0, 0));
		view.setReplyCount(messageDao.queryNewMessageCount(userId, 0, 1));
		view.setSystemCount(0);
		return new GeneralResult<MessageView>().setValue(view);
	}

	/**
	 * 查询用户消息内容
	 * 
	 * @param userId
	 *            当前登录用户
	 * @return
	 */
	@GetMapping("/message/query")
	@ResponseBody
	public ListResult<Message> queryMessage(Long userId, Integer state, Integer type, Integer start, Integer limit) {
		List<Message> mlist = messageDao.queryNewMessage(userId, state, type, start, limit);

		// 填充是否关注
		messageService.isLike(mlist, userId);

		// 填充帖子
		for (Message msg : mlist) {
			msg.setTopic(beanDao.quickQueryOneById(Topic.class, msg.getTopicId()));
		}

		return new ListResult<Message>().setList(mlist);
	}

	/**
	 * 更新状态
	 * 
	 * @param userId
	 *            当前登录用户
	 */
	@PutMapping("/message/state")
	@ResponseBody
	public BaseResult queryNewReply(Long userId, Integer type) {
		// 更新message
		messageDao.updateMessage(userId, type);

		return new BaseResult();
	}

}
